// 问题：实现一个方法，参数为promise数组
// 实现按传入顺序调用
const time = duration => {
  return new Promise(resolve => {
    setTimeout(() => {
      resolve()
    }, duration)
  })
}
const ajax1 = () => time(5000).then(() => {
  console.log(1);
  return 1
})
const ajax2 = () => time(1000).then(() => {
  console.log(2);
  return 2
})
const ajax3 = () => time(1000).then(() => {
  console.log(3);
  return 3
})
const mergePromise = (pList) => {
  const data = []
  let sequence = Promise.resolve()
  pList.forEach(item => {
    sequence = sequence.then(item).then(res => {
      data.push(res)
      return data
    })
  });
  return sequence
}
// mergePromise([ajax1, ajax2, ajax3]).then(res => {
//   console.log(res);
//   console.log('done');
// })

const time1 = (delay) => new Promise((resolve, reject) => {
  setTimeout(resolve, delay)
})
const a1 = () => time(5000).then(() => {
  console.log("1");
  return 1
})
const a2 = () => time(1000).then(() => {
  console.log("2");
  return 2
})
const a3 = () => time(3000).then(() => {
  console.log("3");
  return 3
})
const mergePromise1 = (pList) => {
    let data = []
    queue = Promise.resolve()
    pList.forEach(p => {
      queue = queue.then(p).then(res => {
        data.push(res)
        return data
      })
    })
    return queue
}
mergePromise1([a1, a2, a3]).then(res => {
  console.log(res);
  console.log('done');
  
})